<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理员首页</title>
    <link rel="stylesheet" href="/res/frame/layui/css/layui.css">
    <link rel="stylesheet" href="/res/frame/static/css/style.css">
    <link rel="icon" href="/res/frame/static/image/code.png">
</head>
<body class="body">

<div class="layui-row layui-col-space10 my-index-main">

        <div class="layui-col-xs4 layui-col-sm2 layui-col-md2">
            <div class="my-nav-btn layui-clear">
                <div class="layui-col-md5">
                    <button class="layui-btn layui-btn-big layui-btn-danger layui-icon">&#xe756;</button>
                </div>
                <div class="layui-col-md7 tc">
                    <a href="javascript:;" href-url="/res/html/admin/devList.html">
                    <p class="my-nav-text" id="allDev"></p>
                    <p class="my-nav-text layui-elip">设备数</p>
                    </a>
                </div>
            </div>
        </div>

    <div class="layui-col-xs4 layui-col-sm2 layui-col-md2">
        <div class="my-nav-btn layui-clear" data-href="./demo/form.html">
            <div class="layui-col-md5">
                <button class="layui-btn layui-btn-big layui-btn-warm layui-icon">&#xe735;</button>
            </div>
            <div class="layui-col-md7 tc">
                <p class="my-nav-text" id="onLine"></p>
                <p class="my-nav-text layui-elip">在线设备数</p>
            </div>
        </div>
    </div>
    <div class="layui-col-xs4 layui-col-sm2 layui-col-md2">
        <div class="my-nav-btn layui-clear" data-href="./demo/form.html">
            <div class="layui-col-md5">
                <button class="layui-btn layui-btn-big layui-btn-warm layui-icon">&#xe735;</button>
            </div>
            <div class="layui-col-md7 tc">
                <p class="my-nav-text" id="allSwitch"></p>
                <p class="my-nav-text layui-elip">交换机数</p>
            </div>
        </div>
    </div>
    <div class="layui-col-xs4 layui-col-sm2 layui-col-md2">
        <div class="my-nav-btn layui-clear" data-href="./demo/form.html">
            <div class="layui-col-md5">
                <button class="layui-btn layui-btn-big layui-btn-warm layui-icon">&#xe735;</button>
            </div>
            <div class="layui-col-md7 tc">
                <p class="my-nav-text" id="allCamera"></p>
                <p class="my-nav-text layui-elip">摄像头个数</p>
            </div>
        </div>
    </div>

    <div class="layui-col-xs4 layui-col-sm2 layui-col-md2">
        <div class="my-nav-btn layui-clear" data-href="./demo/table.html">
            <div class="layui-col-md5">
                <button class="layui-btn layui-btn-big layui-icon">&#xe715;</button>
            </div>
            <div class="layui-col-md7 tc">
                <p class="my-nav-text" id="allAlarm"></p>
                <p class="my-nav-text layui-elip">告警个数</p>
            </div>
        </div>
    </div>
    <div class="layui-col-xs4 layui-col-sm2 layui-col-md2">
        <div class="my-nav-btn layui-clear" data-href="./demo/tab-card.html">
            <div class="layui-col-md5">
                <button class="layui-btn layui-btn-big layui-btn-normal layui-icon">&#xe705;</button>
            </div>
            <div class="layui-col-md7 tc">
                <p class="my-nav-text" id="dealAlarm"></p>
                <p class="my-nav-text layui-elip">处理告警个数</p>
            </div>
        </div>
    </div>

    <div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
        <div class="layui-collapse" lay-filter="main-line">
            <div class="layui-colla-item">
                <h2 class="layui-colla-title">CPU使用率</h2>
                <div class="layui-colla-content">
                    <div id="main-line" style="height: 300px"></div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
        <div class="layui-collapse" lay-filter="main-line2">
            <div class="layui-colla-item">
                <h2 class="layui-colla-title">内存使用率</h2>
                <div class="layui-colla-content ">
                    <div id="main-line2" style="height: 300px;"></div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
        <div class="layui-collapse">
            <div class="layui-colla-item">
                <h2 class="layui-colla-title">告警信息</h2>
                <div class="layui-colla-content layui-show">
                    <table class="layui-hide" id="dateTable" lay-filter="devList"></table>
                </div>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript" src="/res/frame/layui/layui.js"></script>
<script type="text/javascript" src="/res/js/index.js"></script>
<script type="text/javascript" src="/res/frame/echarts/echarts.min.js"></script>
<script src="/lib/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="/res/frame/layui/layui.js"></script>
<script type="text/javascript" src="/res/frame/static/js/vip_comm.js"></script>
<script type="text/javascript">
    layui.use(['table', 'form', 'layer', 'vip_table', 'cookie', 'laydate', 'element'], function () {

        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery,
            cookie = layui.cookie,
            element = layui.element,
            laydate = layui.laydate;


        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: vipTable.get43Height()
            , cols: [[
                {type: 'numbers', fixed: 'left', title: '序号'}
                , {field: 'alDevName', title: '设备名', width: 150}
                , {field: 'pkGuid', hide: true}
                , {
                    field: 'alSendStatus', title: '发送状态', width: 100, templet: function (d) {
                        return d.alSendStatus == "SENT" ? "已发送" : "未发送";
                    }
                }
                , {
                    field: 'alType', title: '设备类型', width: 120, templet: function (d) {
                        return d.alType == "SwitchType" ? "交换机" : "摄像头";
                    }
                }
                , {
                    field: 'alLevel', title: '告警等级', width: 120, templet: function (d) {
                        return d.alLevel == "AL_COMMON" ? "一般告警" : "严重告警";
                    }
                }
                , {field: 'alMsg', title: '告警信息', width: 500}
                , {field: 'alCreateDate', title: '产生时间', width: 180}
            ]]
            , id: 'tableReload'
            , url: '/alarm/listByPage'
            , method: 'post'
            , page: true
            , cellMinWidth: 80
            , contentType: 'application/json'
            , request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                }
            }, response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: "200" //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            , loading: false
            , limits: [20, 40, 60]
            , limit: 20 //默认采用30
            , done: function (res, curr, count) {
            }
        });


        $('.my-btn-box .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 刷新
        $('#btn-refresh').on('click', function () {
            tableIns.reload();
        });

        table.on('tool(userList)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'deal') {
                layer.confirm('确认处理告警', function (index) {
                    deal(data.pkGuid);
                });
            } else if (layEvent === 'del') {
                layer.confirm('确认删除告警', function (index) {
                    del(data.pkGuid);
                });
            }
        });

        //init();
        function del(pkGuid) {
            $.ajax({
                url: '/alarm/delAlarm?pkGuid=' + pkGuid,
                type: "get",
                contentType: "application/json",
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {

                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

        function deal(pkGuid) {
            var params = {
                "pkGuid": pkGuid,
                "alStatus": 'YCL',
                "alDlName": $.cookie('adminName'),
                "alDlId": $.cookie('adminId')

            }
            $.ajax({
                url: '/alarm/optionAlarm',
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {
                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

        init();
        // 使用刚指定的配置项和数据显示图表。
        var option = {
            tooltip: {
                formatter: "{a} <br/>{b} : {c}%"
            },
            series: [{
                name: 'cpu使用率',
                type: 'gauge',
                detail: {
                    formatter: '{value}%'
                },
                data: [{
                    value: 0,
                    name: 'cpu使用率'
                }]
            }]
        };


        // 使用刚指定的配置项和数据显示图表。
        var option2 = {
            tooltip: {
                formatter: "{a} <br/>{b} : {c}%"
            },
            series: [{
                name: '内存使用率',
                type: 'gauge',
                detail: {
                    formatter: '{value}%'
                },
                data: [{
                    value: 0,
                    name: '内存使用率'
                }]
            }]
        };

        function init() {
            $.ajax({
                type: "get",
                dataType: "json",
                contentType: "application/json",
                url: "/dev/initData",
                success: function (result) {
                    if (result.code == 200) {
                        $("#allDev").text(result.data.allDev);
                        $("#allSwitch").text(result.data.allSwitch);
                        $("#onLine").text(result.data.onLine);
                        $("#allCamera").text(result.data.allCamera);
                        $("#allAlarm").text(result.data.allAlarm);
                        $("#dealAlarm").text(result.data.dealAlarm);
                        option.series[0].data[0].value = result.data.cup;
                        option2.series[0].data[0].value = result.data.me;
                    } else if (result.code == 500) {

                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    var resultJson = eval('(' + XMLHttpRequest.responseText + ')');
                    layer.msg("error")
                }
            })
        }


        setInterval(function () {
            init();
            tableIns.reload();
        }, 5000);

        // myChart.setOption(option)
        // myChart2.setOption(option2)
        // 基于准备好的dom，初始化echarts实例
        element.on('collapse(main-line)', function (data) {
            //layer.msg('展开状态：'+ data.show);
            var myChart = echarts.init(document.getElementById('main-line'));
            myChart.setOption(option);
            init();
        });
        element.on('collapse(main-line2)', function (data) {
            //layer.msg('展开状态：'+ data.show);
            var myChart2 = echarts.init(document.getElementById('main-line2'));
            myChart2.setOption(option2);
            init();
        });
    });


</script>
</body>
</html>